import { fetchHalls } from '../../services/store/store'
import { getResolutionRatio } from '../../utils/resolution';

let bannerRatio = getResolutionRatio('banner', 0);


Page({

  /**
   * 页面的初始数据
   */
  data: {
    swiperHeight: bannerRatio.height,
    sid: '',
    id: '',
    type: '',
    halls: [],
    currentHall: {
      id: '',
      title: '',
      titlepic: '',
      address: '',
      tables: '',
      pillar: '',
      height: '',
      area: '',
      shape: '',
      banquet_banner: [],
      banquet_album: [],
      birthday_banner: [],
      birthday_album: [],
      meeting_banner: [],
      meeting_album: [],
    },
    currentBanner: [],
    currentAlbum: [],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

    this.setData({
      id: options.id,
      type: options.type,
      sid: options.sid,
    });

    this.fetchHallsData(options.sid)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },

  fetchHallsData(sid) {
    fetchHalls(sid).then(({ halls }) => {
      // 获取id对应的hall
      const currentHall = halls.find((hall) => hall.id == this.data.id);
      // 切换当前选中的banner和album
      const currentBanner = currentHall[`${this.data.type}_banner`] || [];
      const currentAlbum = currentHall[`${this.data.type}_album`] || [];

      this.setData({
        halls: halls,
        currentHall: currentHall || {},
        currentBanner: currentBanner,
        currentAlbum: currentAlbum,
      });
    });
  },

  switchHall(e) {
    const { id } = e.currentTarget.dataset;
    // 切换当前选中的hall
    const currentHall = this.data.halls.find((hall) => hall.id == id);
    // 切换当前选中的banner和album
    const currentBanner = currentHall[`${this.data.type}_banner`] || [];
    const currentAlbum = currentHall[`${this.data.type}_album`] || [];
    this.setData({
      id: id,
      currentHall: currentHall || {},
      currentBanner: currentBanner,
      currentAlbum: currentAlbum,
    });
  },

  previewImage(e) {
    const { type } = e.currentTarget.dataset;
    const urls = type === 'album' ? this.data.currentAlbum : this.data.currentBanner;
    wx.previewImage({
      urls: urls.map((item) => item.url),
    })
  },

  // 返回上一页
  goBack() {
    wx.navigateBack()
  },
})